/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQueries( {
    @NamedQuery(name="findTimeForPersons",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) n, Search sp, IN (sp.searchLevels) lp, IN (lp.nodes) np, Time t WHERE sp = :personSearch AND np = t.person AND s = :search AND t.activity = n AND t.startTime >= :start AND t.startTime < :stop"),
    @NamedQuery(name="findAllTimeForPersons",
                query="SELECT DISTINCT t FROM Search sp, IN (sp.searchLevels) lp, IN (lp.nodes) np, Time t WHERE sp = :personSearch AND np = t.person AND t.startTime >= :start AND t.startTime < :stop"),
    @NamedQuery(name="findTimeByDate",
                query="SELECT DISTINCT t FROM Search s, IN (s.searchLevels) l, IN (l.nodes) n, Time t WHERE t.activity = n AND s = :search AND t.startTime >= :start AND t.startTime < :stop"),
    @NamedQuery(name="findActivityTimeBeforeDate",
                query="SELECT DISTINCT t FROM Activity s, IN (s.times) t WHERE s = :activity AND t.stopTime < :date"),
    @NamedQuery(name="findActivityTimeAfterDate",
                query="SELECT DISTINCT t FROM Activity s, IN (s.times) t WHERE s = :activity AND t.startTime >= :date"),
    @NamedQuery(name="findPersonTimeByDate",
                query="SELECT DISTINCT t FROM Time t WHERE t.person=:person AND t.startTime >= :start AND t.startTime < :stop ORDER BY t.stopTime")
})
public class Time implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String description;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date startTime;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date stopTime;

    private int remainingDuration_min;

    @ManyToOne
    private Activity activity;

    @ManyToOne
    private State state;

    @ManyToOne
    private Person person;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getStartTime() {
        return startTime;
    }

    public Date getStopTime() {
        return stopTime;
    }

    public Person getPerson() {
        return person;
    }

    public String getDescription() {
        return description;
    }

    public State getState() {
        return state;
    }

    public Activity getActivity() {
        return activity;
    }

    public long getDuration() {
        return getStopTime().getTime() - getStartTime().getTime();
    }

    public int getRemainingDuration_min() {
        return remainingDuration_min;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public void setRemainingDuration_min(int remainingDuration_min) {
        this.remainingDuration_min = remainingDuration_min;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setState(State state) {
        this.state = state;
    }

    public void setStopTime(Date stopTime) {
        this.stopTime = stopTime;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Time)) {
            return false;
        }
        Time other = (Time) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.velocityme.entity.Time[id=" + id + "]";
    }

}
